package bdsimple.impl.mysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import bdsimple.abstractfactory.AbstractFactoryDAO;
import bdsimple.dao.base.TarjetaMemoriaDAO;
import bdsimple.dao.base.TarjetaMemoriaIntermediaDAO;
import bdsimple.dto.base.TarjetaMemoriaDTO;
import bdsimple.dto.base.TarjetaMemoriaIntermediaDTO;
import bdsimple.dto.base.VampiroDTO;
import dao.base.api.IDAO;
import dao.base.api.IDTO;

public class TarjetaMemoriaIntermediaDAOImpl extends MySQLBaseDAO implements TarjetaMemoriaIntermediaDAO {

	public TarjetaMemoriaIntermediaDAOImpl() {
		super(TarjetaMemoriaIntermediaDTO.class);
		daoParentClass = TarjetaMemoriaDAO.class;
	}

	// --------------------------------------------------------------------------------
	// MySQLBaseDAO
	// --------------------------------------------------------------------------------
	
	@Override
	protected String createTableColumns() throws Exception {
		
		StringBuffer strbuf = new StringBuffer();

	    strbuf.append(TarjetaMemoriaIntermediaDTOImpl.ID);
	    strbuf.append(" INT PRIMARY KEY, ");
	    strbuf.append(TarjetaMemoriaIntermediaDTOImpl.PUNTOS_ENTRENAMIENTO);
	    strbuf.append(" INT ");
		
	    return strbuf.toString();
	    
	}
	
	@Override
	protected String createInsertCollst(IDTO dto) throws Exception {

		StringBuffer strbuf = new StringBuffer();

	    strbuf.append(TarjetaMemoriaIntermediaDTOImpl.ID);
	    strbuf.append(", ");
	    strbuf.append(TarjetaMemoriaIntermediaDTOImpl.PUNTOS_ENTRENAMIENTO);
	    
	    return strbuf.toString();
		
	}

	@Override
	protected String createInsertValues(IDTO dto) throws Exception {
		
		TarjetaMemoriaIntermediaDTOImpl tarjetaMemoriaIntermediaDTOImpl = // 
			(TarjetaMemoriaIntermediaDTOImpl) dto;

		StringBuffer strbuf = new StringBuffer();

		strbuf.append(tarjetaMemoriaIntermediaDTOImpl.getId());
	    strbuf.append(", ");
		strbuf.append(tarjetaMemoriaIntermediaDTOImpl.getPuntosEntrenamiento());

		return strbuf.toString();
		
	}

	@Override
	protected String createUpdateValues(IDTO dto) throws Exception {
		
		TarjetaMemoriaIntermediaDTOImpl tarjetaMemoriaIntermediaDTOImpl = // 
			(TarjetaMemoriaIntermediaDTOImpl) dto;

		StringBuffer strbuf = new StringBuffer();

		strbuf.append(TarjetaMemoriaIntermediaDTOImpl.PUNTOS_ENTRENAMIENTO);
		strbuf.append(" = ");
		strbuf.append(tarjetaMemoriaIntermediaDTOImpl.getPuntosEntrenamiento());

		return strbuf.toString();
		
	}
	
	// --------------------------------------------------------------------------------
	// IDAO
	// --------------------------------------------------------------------------------

	@Override
	public List<IDTO> listAll(int lim, int off) throws Exception {

		StringBuffer strbuf = new StringBuffer();

		IDAO par0 = //
			AbstractFactoryDAO.getFactoryDAO().getDAO( //
					daoParentClass, connectionBean);

		strbuf.append("SELECT * FROM ");
		strbuf.append(this.getTableName());
		strbuf.append(", ");
		strbuf.append(par0.getTableName());

		strbuf.append(" WHERE ");
		strbuf.append( //
				/**/par0.getTableName() + "." + TarjetaMemoriaDTO.ID + //
				" = " + //
				this.getTableName() + "." + TarjetaMemoriaIntermediaDTO.ID);

		if (lim >= 0 && off >= 0) {
			strbuf.append(" LIMIT  ");
			strbuf.append(lim);
			strbuf.append(" OFFSET ");
			strbuf.append(off);
		}

		System.err.println(strbuf.toString());

		ResultSet rs = //
			connection.createStatement().executeQuery(strbuf.toString());

		List<IDTO> ret = new ArrayList<IDTO>();

		while (rs.next()) {
			ret.add(resultSetToDTO(rs));
		}

		return ret;
	}

	// --------------------------------------------------------------------------------

	@Override
	public List<IDTO> listBy(String key, Object val) throws Exception {

		if (key == null || val == null) {
			throw new IllegalArgumentException("key == null || val == null");
		}

		StringBuffer strbuf = new StringBuffer();

		IDAO par0 = //
			AbstractFactoryDAO.getFactoryDAO().getDAO( //
					daoParentClass, connectionBean);

		strbuf.append("SELECT * FROM ");
		strbuf.append(this.getTableName());
		strbuf.append(", ");
		strbuf.append(par0.getTableName());

		strbuf.append(" WHERE ");
		strbuf.append(key);
		strbuf.append(" = ");
		strbuf.append(val);

		strbuf.append(" AND ");
		strbuf.append( par0.getTableName() + "." + TarjetaMemoriaDTO.ID + //
				" = " + //
				this.getTableName() + "." + TarjetaMemoriaIntermediaDTO.ID);

		System.err.println(strbuf.toString());

		ResultSet rs = //
			connection.createStatement().executeQuery(strbuf.toString());

		List<IDTO> ret = new ArrayList<IDTO>();

		while (rs.next()) {
			ret.add(resultSetToDTO(rs));
		}

		return ret;
	}

	// --------------------------------------------------------------------------------

	@Override
	protected IDTO internalResultSetToDTO(ResultSet rs, IDTO dto)
			throws Exception {
		
		TarjetaMemoriaIntermediaDTOImpl ret = // 
			(TarjetaMemoriaIntermediaDTOImpl) dto;

	    ret.setPuntosEntrenamiento(rs.getInt(TarjetaMemoriaIntermediaDTOImpl.PUNTOS_ENTRENAMIENTO));
	    
	    return ret;
		
	}
	
	public List<TarjetaMemoriaDTO> listAllTarjetaMemoria(int idVampiro) throws SQLException, Exception{
		
		TarjetaMemoriaVampiroDTOImpl tarjetaMemoriaVampiroDTOImpl = new TarjetaMemoriaVampiroDTOImpl(); 
		
		StringBuffer strbuf = new StringBuffer();
		
		strbuf.append("SELECT ");
        strbuf.append(getTableName() + ".*");
        strbuf.append(" FROM ");
        strbuf.append(getTableName());
        strbuf.append(" JOIN ");
        strbuf.append(singleQuotes(tarjetaMemoriaVampiroDTOImpl.getClass().getSimpleName()));
        strbuf.append(" AS dtoClass ");
        strbuf.append(" ON ");
        strbuf.append(getTableName() + "." + VampiroDTO.ID);
        strbuf.append(" = ");
        strbuf.append(idVampiro );

        System.err.println(strbuf.toString());
		
		ResultSet rs = //
			connection.createStatement().executeQuery(strbuf.toString());
		
		List<TarjetaMemoriaDTO> ret = new ArrayList<TarjetaMemoriaDTO>();
		
		while (rs.next()) {
			ret.add((TarjetaMemoriaDTO) resultSetToDTO(rs));
		}

		return ret;
	}
	

}
